package com.unei.guava.demo.bujingnian;

import com.google.common.base.Function;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by sunlei on 2015/1/22.
 */
public class MsgProcessStrategy implements Function<Msg,Msg> {

    List<MsgRule> rules=new LinkedList<MsgRule>();

    public MsgProcessStrategy addRule(MsgRule rule){
        rules.add(rule);
        return this;
    }

    @Override
    public Msg apply(Msg input) {
        Msg after=input;
        for(MsgRule rule:rules){
            if(rule.accept(after)){
                after=rule.process(after);
            }

            if(!rule.goon())
                return after;
        }

        return after;
    }
}
